package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.system.sys.model.dto.SysRoleDto;
import com.yungu.swift.system.sys.model.dto.SysRoleResourceDto;
import com.yungu.swift.system.sys.model.param.AdminLevelPageParam;
import com.yungu.swift.system.sys.model.param.AdminRoleParam;
import com.yungu.swift.system.sys.model.param.AdminRoleResourceAddParam;
import com.yungu.swift.system.sys.model.param.AdminRoleResourceParam;
import com.yungu.swift.system.sys.service.SysRoleResourceService;
import com.yungu.swift.system.sys.service.SysRoleService;
import com.yungu.swift.system.sys.service.SysUserService;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author : cuixiuyin
 * @date : 2019/6/14
 */
@RestController
@RequestMapping("/admin/v1/system/permission/role")
@Api(value = "【系统服务-权限设置中心】", tags = "角色操作相关接口")
public class RoleAdmin {

    @Reference
    private SysUserService sysUserService;
    @Reference
    private SysRoleService sysRoleService;
    @Reference
    private SysRoleResourceService sysRoleResourceService;


    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "角色信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<SysRoleDto>> queryPage(@RequestBody AdminLevelPageParam pageParam) {
        return sysRoleService.queryPage(pageParam);
    }

    @RequestMapping(value = "/resource", method = RequestMethod.POST)
    @ApiOperation(value = "角色对应的资源列表", httpMethod = "POST")
    public ResponseData<PageVo<SysRoleDto>> resource(@RequestBody AdminRoleResourceParam roleResourceParam) {
        return sysRoleResourceService.list(BeanMap.create(roleResourceParam));
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "角色新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> add(@RequestBody AdminRoleParam adminRoleParam) {
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtils.copyProperties(adminRoleParam, sysRoleDto);
        // 新增
        if (StringUtils.isEmpty(adminRoleParam.getUuid())) {
            sysRoleDto.setUuid(StringUtils.buildUUID());
            sysRoleDto.setAgentUuid(adminRoleParam.getUserAgentUuid());
            sysRoleDto.setCompanyUuid(adminRoleParam.getUserCompanyUuid());
            sysRoleDto.setType(SysRoleDto.TYPE_NEW);
            sysRoleDto.setCreateBy(adminRoleParam.getUserUuid());
            sysRoleDto.setCreateOn(new Date());
            return sysRoleService.add(sysRoleDto);
        }
        // 更新
        sysRoleDto.setUpdateOn(new Date());
        sysRoleDto.setUpdateBy(adminRoleParam.getUserUuid());
        return sysRoleService.edit(sysRoleDto);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation(value = "角色删除", httpMethod = "POST")
    public ResponseData<Boolean> del(@RequestBody AdminUuidParam uuidParam) {
        if (ParamUtil.isIllegal(uuidParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        //删除 roleResource 关联
        SysRoleResourceDto paramDto = new SysRoleResourceDto();
        paramDto.setRoleUuid(uuidParam.getUuid());
        ResponseData<Boolean> delRoleResource = sysRoleResourceService.del(paramDto);
        ResponseData<Boolean> del = sysRoleService.del(uuidParam.getUuid(), SysRoleDto.class);
        return ResponseData.buildSuccessResponse(delRoleResource.isSuccess() && del.isSuccess());
    }

    @RequestMapping(value = "/assignResource", method = RequestMethod.POST)
    @ApiOperation(value = "角色分配资源", httpMethod = "POST")
    public ResponseData<Boolean> assignResource(@RequestBody AdminRoleResourceAddParam roleResourceAddParam) {
        if (ParamUtil.isIllegal(roleResourceAddParam.getRoleUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysRoleResourceService.assignResource(roleResourceAddParam);
    }

}
